class Solution {
public:
    int calculate(string s) {
        vector<int> vec;    // 先定义一个数组，模拟栈结构
        char op = '+';      // 保存运算符，初始化为 +
        int tmp = 0;        // 存储的是字符串中的遍历数字
        for (int i = 0; i < s.size(); i++)
        {
            while(s[i] == ' ') i++; // 遇到空格就继续往前走

            while (s[i] >= '0' && s[i] <= '9')
            {
                tmp += (s[i] - '0');
                if (s[i + 1] >= '0' && s[i + 1] <= '9')
                    tmp *= 10;
                i++;
            }

            if (op == '+')
                vec.push_back(tmp);
            else if (op == '-')
                vec.push_back(-tmp);
            else if (op == '*')
                vec.back() *= tmp;
            else if(op == '/' && tmp != 0)
                vec.back() /= tmp;

            if (s[i] == '+')
                op = '+';
            else if (s[i] == '-')
                op = '-';
            else if (s[i] == '*')
                op = '*';
            else
                op = '/';
            tmp = 0;
        }

        int ret = 0;
        for (const auto& e : vec)
            ret += e;

        return ret;
    }
};